package com.example.naodian.utiles;

import com.example.naodian.edu.ucsd.sccn.LSL;
import com.example.naodian.edu.ucsd.sccn.LSL.StreamInfo;
import com.example.naodian.edu.ucsd.sccn.LSL.StreamInlet;
import com.example.naodian.edu.ucsd.sccn.LSL.XMLElement;

import java.util.*;

public class LslTool {

    public static void main(String[] args) {
        receiveLslEegData();
    }

    public static void receiveLslEegData() {
        try {
            System.out.println("Resolving an EEG stream...");
            StreamInfo[] streams = LSL.resolve_stream("type", "EEG");

            if (streams.length == 0) {
                System.out.println("No EEG streams found.");
                return;
            }

            // open an inlet
            StreamInlet inlet = new StreamInlet(streams[0]);

            // Get channel names
            XMLElement channelsNode = inlet.info().desc().child("channels");
            if (channelsNode == null) {
                System.out.println("Channels node not found in stream description.");
                return;
            }

            int channelCount = inlet.info().channel_count();
            List<String> channelLabels = new ArrayList<>();

            XMLElement channelNode = channelsNode.child("channel");
            if (channelNode == null) {
                System.out.println("Channel node not found in channels description.");
                return;
            }

            for (int i = 0; i < channelCount; i++) {
                channelLabels.add(channelNode.child_value("label"));
                channelNode = channelNode.next_sibling();
            }

            System.out.println("Channel Names: " + channelLabels);

            // Define the channels of interest
            List<String> selectedChannels = Arrays.asList("P3", "Pz", "P4", "O1", "Oz", "O2");
            Map<String, Integer> channelIndices = new HashMap<>();

            // Find the indices of the channels of interest
            for (int i = 0; i < channelLabels.size(); i++) {
                if (selectedChannels.contains(channelLabels.get(i))) {
                    channelIndices.put(channelLabels.get(i), i);
                }
            }

            // Check if all channels of interest are found
            if (channelIndices.size() != selectedChannels.size()) {
                System.out.println("Not all channels of interest are found.");
                return;
            }

            // receive data
            double[] sample = new double[inlet.info().channel_count()];
            double baseTimestamp = -1.0;

            while (true) {
                double timestamp = inlet.pull_sample(sample);
                if (baseTimestamp == -1.0) {
                    baseTimestamp = timestamp;
                }
                double elapsedTime = timestamp - baseTimestamp;
                for (String channel : selectedChannels) {
                    System.out.print(channel + ": " + sample[channelIndices.get(channel)] + "\t");
                }
                System.out.println("Elapsed Time: " + elapsedTime);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
